package com.example.yueliang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.yueliang.common.Result;
import com.example.yueliang.dto.FoodViewHotelDto;
import com.example.yueliang.pojo.*;
import com.example.yueliang.service.impl.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 景点  与  美食 Controller
 */
@RestController
@Slf4j
public class CityQueryController {

    @Autowired
    private FoodServiceImpl foodService;

    @Autowired
    private ViewServiceImpl viewService;

    @Autowired
    private HotelServiceImpl hotelService;

    @Autowired
    private CityServiceImpl cityService;

    @Autowired
    private CommentServiceImpl commentService;

    @Autowired
    private UserServiceImpl userService;

    @GetMapping("/index/{cid}")
    public Result queryWithFoodAndView(@PathVariable("cid") Integer cid){
        //查询美食
        LambdaQueryWrapper<Food> foodQueryWrapper = new LambdaQueryWrapper<>();
        foodQueryWrapper.eq(Food::getCid,cid);
        List<Food> foodList = foodService.list(foodQueryWrapper);

        //查询景点
        LambdaQueryWrapper<View> viewQueryWrapper = new LambdaQueryWrapper<>();
        viewQueryWrapper.eq(View::getCid,cid);
        List<View> viewList = viewService.list(viewQueryWrapper);

        LambdaQueryWrapper<City> cityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cityLambdaQueryWrapper.eq(City::getCid,cid);
        City city = cityService.getOne(cityLambdaQueryWrapper);

        //查询酒店  根据城市名模糊查询
        LambdaQueryWrapper<Hotel> hotelQueryWrapper = new LambdaQueryWrapper<>();
        hotelQueryWrapper.like(Hotel::getHotelAddress,city.getCityName());
        List<Hotel> hotelList = hotelService.list(hotelQueryWrapper);

        FoodViewHotelDto foodViewHotelDto = new FoodViewHotelDto();
        foodViewHotelDto.setFoods(foodList);
        foodViewHotelDto.setViews(viewList);
        foodViewHotelDto.setHotels(hotelList);

        //查询评论
        LambdaQueryWrapper<Comment> commentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        commentLambdaQueryWrapper.eq(Comment::getCid,cid);
        commentLambdaQueryWrapper.orderByDesc(Comment::getPublishTime);
        List<Comment> commentList = commentService.list(commentLambdaQueryWrapper);

        foodViewHotelDto.setComments(commentList);

        return Result.success(foodViewHotelDto);
    }

    @PostMapping("/index/comment")
    public Result insertComment(HttpServletRequest request, @RequestBody Comment comment){
        int uid = (int)request.getSession().getAttribute("user");

        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getUid,uid);
        User user = userService.getOne(userLambdaQueryWrapper);

        comment.setUserName(user.getUsername());
        comment.setUid(uid);
        comment.setPublishTime(LocalDateTime.now());

        commentService.save(comment);
        return Result.success(comment);
    }

    @GetMapping("/city/name/{cid}")
    public Result queryCityName(@PathVariable("cid") Integer cid){
        LambdaQueryWrapper<City> cityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cityLambdaQueryWrapper.eq(City::getCid,cid);
        City city = cityService.getOne(cityLambdaQueryWrapper);
        String cityName = city.getCityName();

        return Result.success(cityName);
    }
}
